/**
 * Copyright 2013 Net355 Co,.Ltd. 
 * All rights reserved.
 *
 * "InitEhcacheFile.java is for what: "
 * 
 * Create by Ray at Dec 21, 2013 6:25:42 PM.
 * Ray [email:rayinhangzhou@126.com]
 * 
 * Revision History
 *
 * Date            Programmer                   Notes
 * ---------    ---------------------  -----------------------------------
 * Dec 21, 2013            Ray                       initial
 **/


package com.net355.eblog.test;

import java.util.ArrayList;
import java.util.List;

import com.evernote.auth.EvernoteAuth;
import com.evernote.auth.EvernoteService;
import com.evernote.clients.ClientFactory;
import com.evernote.clients.NoteStoreClient;
import com.evernote.edam.notestore.NoteFilter;
import com.evernote.edam.notestore.NoteMetadata;
import com.evernote.edam.notestore.NotesMetadataList;
import com.evernote.edam.notestore.NotesMetadataResultSpec;
import com.evernote.edam.type.Note;
import com.evernote.edam.type.NoteSortOrder;
import com.evernote.edam.type.Notebook;
import com.evernote.edam.type.Tag;
import com.net355.eblog.models.EbNote;
import com.net355.eblog.models.EbNotebook;
import com.net355.eblog.models.EbTag;
import com.net355.eblog.util.CacheHelper;
import com.net355.eblog.util.DateToolkit;
import com.net355.eblog.util.GlobalParams;
import com.net355.eblog.util.StringUtil;

public class InitEhcacheFiles {

	private static EvernoteAuth evernoteAuth;
	private static ClientFactory factory;
	private static NoteStoreClient noteStore;

	public static void main(String[] args) {
		try {
			evernoteAuth = new EvernoteAuth(EvernoteService.PRODUCTION, GlobalParams.AUTH_TOKEN);
			factory = new ClientFactory(evernoteAuth);
			noteStore = factory.createNoteStoreClient();
//			CacheHelper.initEhCache(null);
//			System.out.println("ray_print-sign-->"+CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_UPDATE));
//			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_TAG)==null) {
//				CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_TAG, new ArrayList<String>());
//			}
//			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTEBOOK)==null) {
//				CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_NOTEBOOK, new ArrayList<String>());
//			}
//			if(CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTE)==null) {
//				CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_NOTE, new ArrayList<String>());
//			}
//			CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_UPDATE, 0);
//			System.out.println("ray_print-started-->");
			
//			CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_SEARCH, 0);
//			CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_VIEW, 0);
//			CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_CHAT, 0);
//			CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_CHAT_SYSTEM, new ArrayList<EbChat>());
//			System.out.println("ray_print-finished-->");
			
			//do the choice
//			updateNoteCmt();
//			geneFiles();
//			showBooks();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}
	}
	
	public static void showBooks(){
		try {
			List<Notebook> notebooks = noteStore.listNotebooks();
			for (Notebook notebook : notebooks) {
				System.out.println("getGuid-->"+notebook.getGuid());
				System.out.println("getName-->"+notebook.getName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public static void geneFiles() throws Exception {
		List<Notebook> notebooks = noteStore.listNotebooks();
		List<String> cacheNotebookList = (List<String>)CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTEBOOK);
		for (Notebook notebook : notebooks) {
			if(!GlobalParams.NOTEBOOK_FILTER.containsKey(notebook.getGuid())) {
				continue;
			}
			System.out.println("notebook-->"+notebook);
			EbNotebook _notebook = (EbNotebook)CacheHelper.get_cache(notebook.getGuid());
			if(_notebook!=null) {
				_notebook.setName(notebook.getName());
				_notebook.setUpdateTime(notebook.getServiceUpdated());
				cacheNotebookList.remove(notebook.getGuid());
			} else {
				_notebook = new EbNotebook();
				_notebook.setGuid(notebook.getGuid());
				_notebook.setName(notebook.getName());
				_notebook.setCreateTime(notebook.getServiceCreated());
				_notebook.setUpdateTime(notebook.getServiceUpdated());
			}
			CacheHelper.set_cache(notebook.getGuid(), _notebook);
			cacheNotebookList.add(notebook.getGuid());
		}
		CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_NOTEBOOK, cacheNotebookList);
		
		int offset = 0;
		int step = 0;
		int save = 0;
		NoteFilter filter = new NoteFilter();
		filter.setOrder(NoteSortOrder.UPDATED.getValue());
		filter.setAscending(true);
		NotesMetadataResultSpec resultSpec = new NotesMetadataResultSpec();
		resultSpec.setIncludeNotebookGuid(true);
		while (true) {
			NotesMetadataList noteList = noteStore.findNotesMetadata(filter, offset, 200, resultSpec);
			System.out.println("result-->"+noteList.getNotesSize());
			offset += noteList.getNotesSize();
			if(noteList.getNotesSize()==0) {
				break;
			} else {
				for (NoteMetadata note : noteList.getNotes()) {
					System.out.println("step-->"+step);
					step++;
					if(!GlobalParams.NOTEBOOK_FILTER.containsKey(note.getNotebookGuid())) {
						continue;
					}
					System.out.println("start get-->"+step);
					Note detailNote = noteStore.getNote(note.getGuid(), true, false, false, false);
					System.out.println(DateToolkit.formatDate("HH:mm:ss")+"save-->"+save+"-"+detailNote.getTitle());
					save++;
					List<String> cacheTagList = (List<String>)CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_TAG);
					for (int i = 0; i < detailNote.getTagGuidsSize(); i++) {	//update new tags with add note
						String tagGuid = detailNote.getTagGuids().get(i);
						EbTag _tag = (EbTag)CacheHelper.get_cache(tagGuid);
						if(_tag==null) {
							Tag tag = noteStore.getTag(tagGuid);
							Thread.sleep(800);
							_tag = new EbTag();
							_tag.setGuid(tagGuid);
							_tag.setName(tag.getName());
						} else {
							cacheTagList.remove(tagGuid);
						}
						_tag.getNotes().add(detailNote.getGuid());
						_tag.setUpdateTime(detailNote.getUpdated());
						CacheHelper.set_cache(tagGuid, _tag);
						cacheTagList.add(tagGuid);
					}
					if(detailNote.getTagGuidsSize()>0) {
						CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_TAG, cacheTagList);
					}
					EbNotebook eNotebook = (EbNotebook)CacheHelper.get_cache(detailNote.getNotebookGuid());
					eNotebook.getNotes().add(detailNote.getGuid());
					eNotebook.setUpdateTime(detailNote.getUpdated());
					CacheHelper.set_cache(eNotebook.getGuid(), eNotebook); 
					cacheNotebookList.remove(detailNote.getNotebookGuid());
					cacheNotebookList.add(detailNote.getNotebookGuid());
					CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_NOTEBOOK, cacheNotebookList);
					EbNote eNote = new EbNote();
					eNote.setGuid(detailNote.getGuid());
					eNote.setTags(detailNote.getTagGuids());
					eNote.setNotebookGuid(detailNote.getNotebookGuid());
					eNote.setTitle(detailNote.getTitle());
					eNote.setUpdateTime(detailNote.getUpdated());
					eNote.setContentHash("none");
					updateNoteContentCache(eNote, detailNote);
					List<String> cacheNoteList = (List<String>)CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTE);
					cacheNoteList.add(detailNote.getGuid());
					CacheHelper.set_cache(GlobalParams.CACHE_NAME_LIST_NOTE, cacheNoteList);
					CacheHelper.set_cache(GlobalParams.CACHE_NAME_COUNT_UPDATE, noteList.getUpdateCount());
				}
			}
		}
		System.out.println("CACHE_NAME_LIST_NOTEBOOK-->"+((List<?>)CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTEBOOK)).size());
		System.out.println("CACHE_NAME_LIST_NOTE-->"+((List<?>)CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTE)).size());
		System.out.println("CACHE_NAME_LIST_TAG-->"+((List<?>)CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_TAG)).size());
		System.out.println("CACHE_NAME_COUNT_UPDATE-->"+CacheHelper.get_cache(GlobalParams.CACHE_NAME_COUNT_UPDATE));
		System.exit(0);
	}
	
	private static void updateNoteContentCache(EbNote note, Note detailNote) {
		try {
			System.out.println("content-->"+note.getTitle());
			note.setContentHash(StringUtil.bytesToHex(detailNote.getContentHash()));
			note.setContentLength(detailNote.getContentLength());
			// add noteContent
			CacheHelper.set_cache_html(note.getGuid(), detailNote.getContent());
			//	add noteText
			String text = StringUtil.parseHTMLtoText(detailNote.getContent());
			List<String> sectionList = new ArrayList<String>();
			int index = GlobalParams.TEXT_SECTION_SIZE;
			int length = text.length();
			while (index<length) {
				System.out.println("index-->"+index);
				sectionList.add(text.substring(index-GlobalParams.TEXT_SECTION_SIZE, index));
				index+=GlobalParams.TEXT_SECTION_SIZE;
			}
			sectionList.add(text.substring(index-GlobalParams.TEXT_SECTION_SIZE));
			CacheHelper.set_cache_text(note.getGuid(), sectionList);
			//	add or update note
			note.setContent(StringUtil.getSubContent(sectionList.get(0), GlobalParams.SIMPLE_CONTENT_SIZE));
			CacheHelper.set_cache(note.getGuid(), note);
			
		} catch (Exception e) {
			System.out.println("updateNoteCache error--->>>");
			e.printStackTrace();
		}
	}
	
	
	public static void updateNoteCmt(){
		try {
			List<?> notes = (List<?>)CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTE);
			for (int i = 0; i < notes.size(); i++) {
				String note = (String)notes.get(i);
				CacheHelper.set_cache(note+GlobalParams.CACHE_NAME_LIST_NOTE_COMMENT, new ArrayList<String>());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	};
	
	
	
	
	
	
	
	
	
	
	
}
